package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	/**
	 * @author gengjian
	 * @date 2025/02/18 09:16:11
	 * @return 结构化分类数据列表，每个元素包含三级分类结构的json对象
	 * 			- categoryId：一级分类ID
	 * 			- categoryName：一级分类名称
	 * 			- categoryChild：二级分类集合	（结构同上）
	 * 	查询所有分类（1,2,3级）并构建嵌套树形结构数据
	 */
    @Override
    public List<JSONObject> getBaseCategoryList() {
		//创建树形结构集合（也是一级分类集合）封装返回结果
		List<JSONObject> list = new ArrayList<>();
		//获取全量平铺分类数据（包含1、2、3级分类信息）
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//按照一级分类ID分组再构建成map结构key=一级分类ID，value=一级分类下所有分类记录
		Map<Long, List<BaseCategoryView>> category1Map =
				baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//遍历map集合以获取一级分类数据及二级分类集合
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = category1Map.entrySet().iterator();
		while (iterator1.hasNext()){
			Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
			//构建一级分类json对象
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("categoryId",entry1.getKey());
			jsonObject.put("categoryName",entry1.getValue().get(0).getCategory1Name());
			//按照二级分类ID分组再构建成map结构key=二级分类ID，value=二级分类下所有分类记录
			Map<Long, List<BaseCategoryView>> category2Map =
					entry1.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//遍历map集合以获取二级分类数据及三级分类集合
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator2 = category2Map.entrySet().iterator();
			//创建二级分类集合
			List<JSONObject> category2List = new ArrayList<>();
			while (iterator2.hasNext()){
				Map.Entry<Long, List<BaseCategoryView>> entry2 = iterator2.next();
				//构建二级分类json对象
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId",entry2.getKey());
				jsonObject2.put("categoryName",entry2.getValue().get(0).getCategory2Name());
				//创建三级分类集合
				List<JSONObject> category3List = new ArrayList<>();
				//循环遍历三级分类数据封装到三级分类集合
				for (BaseCategoryView baseCategoryView : entry2.getValue()) {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId",baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName",baseCategoryView.getCategory3Name());
					category3List.add(jsonObject3);
				}
				jsonObject2.put("categoryChild",category3List);
				category2List.add(jsonObject2);
			}
			jsonObject.put("categoryChild",category2List);
			list.add(jsonObject);
		}
		return list;
    }

	/**
	 * @author gengjian
	 * @date 2025/03/02 11:48:55
	 * 根据一级分类id查所有分类数据
	 */
	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		//构建封装结果对象
		JSONObject jsonObject = new JSONObject();
		//根据视图查询所有数据
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>()
				.eq(BaseCategoryView::getCategory1Id, category1Id));
		//封装一级分类结构
		jsonObject.put("categoryId",category1Id);
		jsonObject.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());
		//获取一级分类集合

		//获取二级分类集合
		Map<Long, List<BaseCategoryView>> category2Map =
				baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		//遍历二级分类集合
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = category2Map.entrySet().iterator();
		//创建集合封装2级分类
		List<JSONObject> list = new ArrayList<>();
		while (iterator.hasNext()){
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			List<BaseCategoryView> categoryView2List = entry.getValue();
			//创建2级结果对象
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("categoryId",entry.getKey());
			jsonObject1.put("categoryName",categoryView2List.get(0).getCategory2Name());
			//创建集合封装3级分类
			//获取三级分类集合
			List<JSONObject> list1 = categoryView2List.stream().map(categoryView -> {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", categoryView.getCategory3Id());
				jsonObject2.put("categoryName", categoryView.getCategory3Name());
				return jsonObject2;
			}).toList();
			jsonObject1.put("categoryChild",list1);
			list.add(jsonObject1);
		}
		jsonObject.put("categoryChild",list);
		return jsonObject;
	}

	/**
	 * @author gengjian
	 * @date 2025/02/18 20:52:30
	 * 根据一级分类ID获取专辑属性值
	 */
	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		//调用mapper层方法
		List<BaseAttribute> baseAttributeList = baseAttributeMapper.selectBaseAttributeList(category1Id);
		return baseAttributeList;
	}
	/**
	 * @author gengjian
	 * @date 2025/02/26 16:04:36
	 * 根据三级分类id获取分类视图
	 */
    @Override
    public BaseCategoryView getBaseCategoryView(Long category3Id) {
		//调用mapper方法
		BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectOne(
				new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory3Id, category3Id));
		return baseCategoryView;
	}


	/**
	 * @author gengjian
	 * @date 2025/02/28 10:05:03
	 * 根据一级分类id查询三级分类列表
	 */
	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//根据一级分类id查二级分类列表
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(
				new LambdaQueryWrapper<BaseCategory2>()
						.eq(BaseCategory2::getCategory1Id, category1Id));
		//将二级分类列表转换为二级分类id列表
		List<Long> category2IdList = baseCategory2List.stream().map(BaseCategory2::getId).toList();
		//根据二级分类id查三级分类列表
		List<BaseCategory3> category3List = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
				.in(BaseCategory3::getCategory2Id, category2IdList)
				.eq(BaseCategory3::getIsTop,1)
				.last(" limit 7 "));
		return category3List;
	}
}
